//
//  WeathCitysCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/27.
//  Copyright © 2016年 game. All rights reserved.
//

#import "WeathCitysCtrl.h"
#import "SelectNavView.h"
#import "ProvinceCtrl.h"
#import "WetherModel.h"
#import "CityModel.h"

@interface WeathCitysCtrl ()
@property (nonatomic,strong) SelectNavView *navView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,assign)  NSInteger count;
@end

@implementation WeathCitysCtrl
-(NSMutableDictionary *)cityDatadic{

    if (!_cityDatadic) {
        _cityDatadic = [NSMutableDictionary dictionary];
    }
    return _cityDatadic;
}

-(UIScrollView *)scrollView{
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        
        [self.view addSubview:_scrollView];
    }

    return _scrollView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CreatNavView];
}

-(void)CreatNavView{
    StatusBlack
    self.view.backgroundColor = [UIColor whiteColor];
    self.navView = [[SelectNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.navView.titile.text = @"城市管理";
    [self.navView.share setTitle:@"编辑" forState:UIControlStateNormal];
    [self.navView.share setTitle:@"保存" forState:UIControlStateSelected];
    self.navView.share.frame = CGRectMake(SCREEN_WIDTH-50, 23, 40, 40);
    [self.navView.share setTitleColor:mainColor forState:UIControlStateNormal];
    self.navView.share.titleLabel.font = font(SCREEN_WIDTH*0.05);
    [self.navView.share addTarget:self action:@selector(CitySet:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navView.bacBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.navView];
}

-(void)back{
    
    if (self.navView.share.selected) {
        [Manager GetHudWhithText:@"请您先对编辑进行保存" AndFatherView:self.view];
    }else{
        
        CATransition *transition = [CATransition animation];
        transition.duration = 0.5f;
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.subtype = kCATransitionFromLeft;
        transition.type = @"oglFlip";
        transition.delegate = self;
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)CitySet:(UIButton *)btn{

    btn.selected = !btn.selected;
    [self setCityArys:self.cityArys];
}

-(void)setCityArys:(NSMutableArray *)cityArys{
    
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _cityArys = cityArys;
    
    if (_cityArys.count==0) {
        self.navView.share.selected=NO;
    }
    
    CGFloat xd = 15;
    CGFloat ww = (SCREEN_WIDTH - xd*5)/4;
    
   self.count = _cityArys.count;
    
    if (self.count<10) {
        self.count = _cityArys.count+1;
    }
    
    for (int i=0; i<self.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSInteger row = i%4;
        NSInteger hor = i/4;
        CGFloat xx = xd +(xd+ww)*row;
        CGFloat yy = xd +(xd+ww)*hor;
        btn.frame = CGRectMake(xx, yy, ww, ww);
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
         btn.titleLabel.font = font(SCREEN_WIDTH*0.047);
        btn.tag = 10000+i;
        UIButton *shanchu = [UIButton buttonWithType:UIButtonTypeCustom];
        shanchu.frame = CGRectMake(ww-25, 0, 25, 25);

        [shanchu setImage:[UIImage imageNamed:@"delete_pic"] forState:UIControlStateNormal];
        [shanchu addTarget:self action:@selector(shanchu:) forControlEvents:UIControlEventTouchUpInside];
       
        if (self.navView.share.selected) {
            shanchu.alpha = 1;
            
        }else{
            shanchu.alpha = 0;
            if (i==0) {
                btn.backgroundColor = mainColor;
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }
        }
        
        if (i<_cityArys.count) {
            
            btn.layer.borderColor = linColor.CGColor;
            btn.layer.borderWidth = 0.6;

            CityModel *model = [[CityModel alloc]initWithDictionary:_cityArys[i] error:nil];
            [btn setTitle:model.name forState:UIControlStateNormal];
            
            shanchu.tag = i+100;
            [btn addSubview:shanchu];
            
        }else{
            
            [btn setImage:[UIImage imageNamed:@"add_pic"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.backgroundColor = [UIColor whiteColor];
            
            [btn addTarget:self action:@selector(citySeleck:)  forControlEvents:UIControlEventTouchUpInside];
        }
        [self.scrollView addSubview:btn];
        self.scrollView.contentSize = CGSizeMake(0, btn.x+xd);
    }
    
    if (!self.navView.share.selected) {
        
        [self.deleget RefreshCitys:_cityArys];
        
        [self.cityArys writeToFile:[Manager CityAryPath] atomically:YES];
    }
}


/**
 *  点击添加城市  只有最后一个btn有效
 */
-(void)citySeleck:(UIButton *)btn{
    
    if (!self.navView.share.selected) {

            ProvinceCtrl *procince = [ProvinceCtrl new];
            
        [self.navigationController pushViewController:procince animated:YES];
    }
    

}

-(void)shanchu:(UIButton *)btn{
    
    NSDictionary *dic = self.cityArys[btn.tag-100];
    [self.cityDatadic removeObjectForKey:dic[@"name"]];
    
    [self.cityArys removeObjectAtIndex:btn.tag-100];
    [self setCityArys:self.cityArys];
    
    if (self.cityArys.count==0) {
        
        CityModel *model = [[CityModel alloc]initWithDictionary:[self.Myud valueForKey:@"CityDic"] error:nil];
        
        [self.cityArys addObject:[self.Myud valueForKey:@"CityDic"]];
        [self AchiveWeatherDataWhithCityName:model.name AndCode:model.code];
    }
}

//获取天气数据
-(void)AchiveWeatherDataWhithCityName:(NSString *)cityname AndCode:(NSString *)citycode{
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:weathUrl,citycode] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        WetherModel *model = [[WetherModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        
        [self.cityDatadic addEntriesFromDictionary:@{cityname:model}];
        [self setCityArys:self.cityArys];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
